/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.dusa.worklog.viewer;

import java.awt.Color;
import java.awt.Component;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.DefaultCellEditor;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.text.DateFormatter;
import org.openide.util.Exceptions;

/**
 *
 * @author vdusa
 */
    class TimeCellEditor extends DefaultCellEditor {

        /**
         * for default system format use DateFormat.getTimeInstance();
         */
        DateFormat format;        
        DateFormat formatShort = new SimpleDateFormat("HH-mm");

        public TimeCellEditor(DateFormat format) {
            super(new JFormattedTextField(new DateFormatter(format)));
            this.format = format;
        }

        @Override
        public Object getCellEditorValue() {
            String text = ((JTextField) getComponent()).getText();
            Time value = parseTime(text);
            return value;
        }

        @Override
        public Component getTableCellEditorComponent(
                final JTable table, final Object value,
                final boolean isSelected, final int row, final int column) {
            JTextField tf = ((JTextField) getComponent());
            tf.setBorder(new LineBorder(Color.black));
            try {
                tf.setText(format.format(value));
            } catch (Exception e) {
                tf.setText("");
            }
            return tf;
        }

        public Time parseTime(String value) {
            Time Time;
            Time = parseTime(value, format);
            if(Time!=null) return Time;

            Time = parseTime(value, formatShort);
            if(Time!=null) return Time;

            return null;
        }

        private Time parseTime(String value, DateFormat format){
            ParsePosition pos= new ParsePosition(0);
            format.parse(value, pos); // updates pos.
            if (pos.getIndex()!=value.length() || format.getCalendar().get(Calendar.YEAR)>2500){
                return null;
            }

            try {
                java.util.Date time = format.parse(value);
                return new Time(time.getTime());
            } catch (ParseException ex) {
                Exceptions.printStackTrace(ex);
            }
            return null;

        }

        @Override
        public boolean stopCellEditing() {
            String value = ((JTextField) getComponent()).getText();
            if (!value.equals("")) {
                if (parseTime(value)==null) {
                    ((JComponent) getComponent()).setBorder(new LineBorder(Color.red));
                    return false;
                }
            }
            return super.stopCellEditing();
        }
    }
